

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/blog/img/fluid.png">
  <link rel="icon" href="/blog/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Fantuan">
  <meta name="keywords" content="">
  
    <meta name="description" content="Vue+AntD切换主题（仅仅切换主题，不包含字体等其他）准备工作：  Vue2.0项目（3.0没试过，应该也适用）、webpack、插件：&amp;#119;&amp;#101;&amp;#98;&amp;#x70;&amp;#x61;&amp;#99;&amp;#107;&amp;#x2d;&amp;#116;&amp;#x68;&amp;#x65;&amp;#109;&amp;#101;&amp;#x2d;&amp;#x63;&amp;#x6f;&amp;#x6c;&amp;#x6f;&amp;#114;&amp;#x2d;&amp;#114;&amp;#101;">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue2+AntD切换主题">
<meta property="og:url" content="https://gitee.com/swallow_week/blog/2022/07/06/%E5%8A%A8%E6%80%81%E5%88%87%E6%8D%A2%E4%B8%BB%E9%A2%98/index.html">
<meta property="og:site_name" content="饭团君的博客">
<meta property="og:description" content="Vue+AntD切换主题（仅仅切换主题，不包含字体等其他）准备工作：  Vue2.0项目（3.0没试过，应该也适用）、webpack、插件：&amp;#119;&amp;#101;&amp;#98;&amp;#x70;&amp;#x61;&amp;#99;&amp;#107;&amp;#x2d;&amp;#116;&amp;#x68;&amp;#x65;&amp;#109;&amp;#101;&amp;#x2d;&amp;#x63;&amp;#x6f;&amp;#x6c;&amp;#x6f;&amp;#114;&amp;#x2d;&amp;#114;&amp;#101;">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-07-07T07:47:35.000Z">
<meta property="article:modified_time" content="2022-07-07T07:48:33.250Z">
<meta property="article:author" content="Fantuan">
<meta property="article:tag" content="antDesign">
<meta name="twitter:card" content="summary_large_image">
  
  
  
  <title>Vue2+AntD切换主题 - 饭团君的博客</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"gitee.com","root":"/blog/","version":"1.9.1","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/blog/js/utils.js" ></script>
  <script  src="/blog/js/color-schema.js" ></script>

  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/blog/">
      <strong>Fluid</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/blog/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Vue2+AntD切换主题"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-07-06 23:47" pubdate>
          2022年7月6日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          7.1k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          60 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Vue2+AntD切换主题</h1>
            
            <div class="markdown-body">
              
              <h4 id="Vue-AntD切换主题（仅仅切换主题，不包含字体等其他）"><a href="#Vue-AntD切换主题（仅仅切换主题，不包含字体等其他）" class="headerlink" title="Vue+AntD切换主题（仅仅切换主题，不包含字体等其他）"></a>Vue+AntD切换主题（仅仅切换主题，不包含字体等其他）</h4><p>准备工作：</p>
<blockquote>
<p>Vue2.0项目（3.0没试过，应该也适用）、webpack、插件：<a href="mailto:&#119;&#101;&#98;&#x70;&#x61;&#99;&#107;&#x2d;&#116;&#x68;&#x65;&#109;&#101;&#x2d;&#x63;&#x6f;&#x6c;&#x6f;&#114;&#x2d;&#114;&#101;&#x70;&#108;&#97;&#x63;&#101;&#x72;&#x40;&#x31;&#x2e;&#x33;&#46;&#49;&#56;">&#119;&#101;&#98;&#x70;&#x61;&#99;&#107;&#x2d;&#116;&#x68;&#x65;&#109;&#101;&#x2d;&#x63;&#x6f;&#x6c;&#x6f;&#114;&#x2d;&#114;&#101;&#x70;&#108;&#97;&#x63;&#101;&#x72;&#x40;&#x31;&#x2e;&#x33;&#46;&#49;&#56;</a>、<a href="mailto:&#97;&#x6e;&#116;&#45;&#100;&#x65;&#115;&#105;&#103;&#x6e;&#x2d;&#x76;&#x75;&#x65;&#64;&#49;&#46;&#x35;&#46;&#x30;&#45;&#x72;&#99;&#x2e;&#54;">&#97;&#x6e;&#116;&#45;&#100;&#x65;&#115;&#105;&#103;&#x6e;&#x2d;&#x76;&#x75;&#x65;&#64;&#49;&#46;&#x35;&#46;&#x30;&#45;&#x72;&#99;&#x2e;&#54;</a></p>
</blockquote>
<p>假设你已经完成准备工作</p>
<ol>
<li><p>首先配置<code>webpack-theme-color-replacer</code>插件<br>随便找个文件夹创建一个<code>plugin.config.js</code>文件，内容如下：</p>
<figure class="highlight roboconf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><code class="hljs roboconf">const ThemeColorReplacer = require(&#x27;webpack-theme-color-replacer&#x27;)<br>const generate = require(&#x27;@ant-design/colors/lib/generate&#x27;).default<br><br>const getAntdSerials = (color) =&gt; &#123;<br>  // 淡化（即<span class="hljs-attribute">less的tint）</span><br><span class="hljs-attribute">  const lightens = new Array(9).fill().map((t, i) =&gt; &#123;</span><br><span class="hljs-attribute">    return ThemeColorReplacer.varyColor.lighten(color, i / 10)</span><br><span class="hljs-attribute">  &#125;)</span><br><span class="hljs-attribute">  const colorPalettes = generate(color)</span><br><span class="hljs-attribute">  const rgb = ThemeColorReplacer.varyColor.toNum3(color.replace(&#x27;#&#x27;, &#x27;&#x27;)).join(&#x27;,&#x27;)</span><br><span class="hljs-attribute">  return lightens.concat(colorPalettes).concat(rgb)</span><br><span class="hljs-attribute">&#125;</span><br><span class="hljs-attribute"></span><br><span class="hljs-attribute">const themePluginOption = &#123;</span><br><span class="hljs-attribute">  fileName</span>: &#x27;css/theme-colors-[contenthash:8]<span class="hljs-variable">.css</span>&#x27;,<br>  matchColors: getAntdSerials(&#x27;#1890ff&#x27;), // 主色系列<br>  // 改变样式选择器，解决样式覆盖问题<br>  changeSelector (selector) &#123;<br>    switch (selector) &#123;<br>      case &#x27;<span class="hljs-variable">.ant-calendar-today</span> <span class="hljs-variable">.ant-calendar-date</span>&#x27;:<br>        return &#x27;:not(<span class="hljs-variable">.ant-calendar-selected-date</span>):not(<span class="hljs-variable">.ant-calendar-selected-day</span>)&#x27; + selector<br>      case &#x27;<span class="hljs-variable">.ant-btn</span>:focus,<span class="hljs-variable">.ant-btn</span>:hover&#x27;:<br>        return &#x27;<span class="hljs-variable">.ant-btn</span>:focus:not(<span class="hljs-variable">.ant-btn-primary</span>):not(<span class="hljs-variable">.ant-btn-danger</span>),<span class="hljs-variable">.ant-btn</span>:hover:not(<span class="hljs-variable">.ant-btn-primary</span>):not(<span class="hljs-variable">.ant-btn-danger</span>)&#x27;<br>      case &#x27;<span class="hljs-variable">.ant-btn</span><span class="hljs-variable">.active</span>,<span class="hljs-variable">.ant-btn</span>:active&#x27;:<br>        return &#x27;<span class="hljs-variable">.ant-btn</span><span class="hljs-variable">.active</span>:not(<span class="hljs-variable">.ant-btn-primary</span>):not(<span class="hljs-variable">.ant-btn-danger</span>),<span class="hljs-variable">.ant-btn</span>:active:not(<span class="hljs-variable">.ant-btn-primary</span>):not(<span class="hljs-variable">.ant-btn-danger</span>)&#x27;<br>      case &#x27;<span class="hljs-variable">.ant-steps-item-process</span> <span class="hljs-variable">.ant-steps-item-icon</span> &gt; <span class="hljs-variable">.ant-steps-icon</span>&#x27;:<br>      case &#x27;<span class="hljs-variable">.ant-steps-item-process</span> <span class="hljs-variable">.ant-steps-item-icon</span>&gt;<span class="hljs-variable">.ant-steps-icon</span>&#x27;:<br>        return &#x27;:not(<span class="hljs-variable">.ant-steps-item-process</span>)&#x27; + selector<br>      case &#x27;<span class="hljs-variable">.ant-menu-horizontal</span>&gt;<span class="hljs-variable">.ant-menu-item-active</span>,<span class="hljs-variable">.ant-menu-horizontal</span>&gt;<span class="hljs-variable">.ant-menu-item-open</span>,<span class="hljs-variable">.ant-menu-horizontal</span>&gt;<span class="hljs-variable">.ant-menu-item-selected</span>,<span class="hljs-variable">.ant-menu-horizontal</span>&gt;<span class="hljs-variable">.ant-menu-item</span>:hover,<span class="hljs-variable">.ant-menu-horizontal</span>&gt;<span class="hljs-variable">.ant-menu-submenu-active</span>,<span class="hljs-variable">.ant-menu-horizontal</span>&gt;<span class="hljs-variable">.ant-menu-submenu-open</span>,<span class="hljs-variable">.ant-menu-horizontal</span>&gt;<span class="hljs-variable">.ant-menu-submenu-selected</span>,<span class="hljs-variable">.ant-menu-horizontal</span>&gt;<span class="hljs-variable">.ant-menu-submenu</span>:hover&#x27;:<br>      case &#x27;<span class="hljs-variable">.ant-menu-horizontal</span> &gt; <span class="hljs-variable">.ant-menu-item-active</span>,<span class="hljs-variable">.ant-menu-horizontal</span> &gt; <span class="hljs-variable">.ant-menu-item-open</span>,<span class="hljs-variable">.ant-menu-horizontal</span> &gt; <span class="hljs-variable">.ant-menu-item-selected</span>,<span class="hljs-variable">.ant-menu-horizontal</span> &gt; <span class="hljs-variable">.ant-menu-item</span>:hover,<span class="hljs-variable">.ant-menu-horizontal</span> &gt; <span class="hljs-variable">.ant-menu-submenu-active</span>,<span class="hljs-variable">.ant-menu-horizontal</span> &gt; <span class="hljs-variable">.ant-menu-submenu-open</span>,<span class="hljs-variable">.ant-menu-horizontal</span> &gt; <span class="hljs-variable">.ant-menu-submenu-selected</span>,<span class="hljs-variable">.ant-menu-horizontal</span> &gt; <span class="hljs-variable">.ant-menu-submenu</span>:hover&#x27;:<br>        return &#x27;<span class="hljs-variable">.ant-menu-horizontal</span> &gt; <span class="hljs-variable">.ant-menu-item-active</span>,<span class="hljs-variable">.ant-menu-horizontal</span> &gt; <span class="hljs-variable">.ant-menu-item-open</span>,<span class="hljs-variable">.ant-menu-horizontal</span> &gt; <span class="hljs-variable">.ant-menu-item-selected</span>,<span class="hljs-variable">.ant-menu-horizontal</span>:not(<span class="hljs-variable">.ant-menu-dark</span>) &gt; <span class="hljs-variable">.ant-menu-item</span>:hover,<span class="hljs-variable">.ant-menu-horizontal</span> &gt; <span class="hljs-variable">.ant-menu-submenu-active</span>,<span class="hljs-variable">.ant-menu-horizontal</span> &gt; <span class="hljs-variable">.ant-menu-submenu-open</span>,<span class="hljs-variable">.ant-menu-horizontal</span>:not(<span class="hljs-variable">.ant-menu-dark</span>) &gt; <span class="hljs-variable">.ant-menu-submenu-selected</span>,<span class="hljs-variable">.ant-menu-horizontal</span>:not(<span class="hljs-variable">.ant-menu-dark</span>) &gt; <span class="hljs-variable">.ant-menu-submenu</span>:hover&#x27;<br>      case &#x27;<span class="hljs-variable">.ant-menu-horizontal</span> &gt; <span class="hljs-variable">.ant-menu-item-selected</span> &gt; a&#x27;:<br>      case &#x27;<span class="hljs-variable">.ant-menu-horizontal</span>&gt;<span class="hljs-variable">.ant-menu-item-selected</span>&gt;a&#x27;:<br>        return &#x27;<span class="hljs-variable">.ant-menu-horizontal</span>:not(ant-menu-light):not(<span class="hljs-variable">.ant-menu-dark</span>) &gt; <span class="hljs-variable">.ant-menu-item-selected</span> &gt; a&#x27;<br>      case &#x27;<span class="hljs-variable">.ant-menu-horizontal</span> &gt; <span class="hljs-variable">.ant-menu-item</span> &gt; a:hover&#x27;:<br>      case &#x27;<span class="hljs-variable">.ant-menu-horizontal</span>&gt;<span class="hljs-variable">.ant-menu-item</span>&gt;a:hover&#x27;:<br>        return &#x27;<span class="hljs-variable">.ant-menu-horizontal</span>:not(ant-menu-light):not(<span class="hljs-variable">.ant-menu-dark</span>) &gt; <span class="hljs-variable">.ant-menu-item</span> &gt; a:hover&#x27;<br>      default :<br>        return selector<br>    &#125;<br>  &#125;<br>&#125;<br><br>const createThemeColorReplacerPlugin = () =&gt; new ThemeColorReplacer(themePluginOption)<br><br>module<span class="hljs-variable">.exports</span> = createThemeColorReplacerPlugin<br><br></code></pre></td></tr></table></figure>

<p>同时创建一个<code>settingConfig.js</code>文件和<code>themeColor.js</code>文件</p>
<p>settingConfig.js</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">import</span> &#123; message &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;ant-design-vue/es&#x27;</span><br><span class="hljs-keyword">import</span> themeColor <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./themeColor.js&#x27;</span><br><span class="hljs-keyword">const</span> colorList = [<br>  &#123;<br>    <span class="hljs-attr">key</span>: <span class="hljs-string">&#x27;薄暮&#x27;</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">&#x27;#F5222D&#x27;</span><br>  &#125;,<br>  &#123;<br>    <span class="hljs-attr">key</span>: <span class="hljs-string">&#x27;火山&#x27;</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">&#x27;#FA541C&#x27;</span><br>  &#125;,<br>  &#123;<br>    <span class="hljs-attr">key</span>: <span class="hljs-string">&#x27;日暮&#x27;</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">&#x27;#FAAD14&#x27;</span><br>  &#125;,<br>  &#123;<br>    <span class="hljs-attr">key</span>: <span class="hljs-string">&#x27;明青&#x27;</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">&#x27;#13C2C2&#x27;</span><br>  &#125;,<br>  &#123;<br>    <span class="hljs-attr">key</span>: <span class="hljs-string">&#x27;极光绿&#x27;</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">&#x27;#52C41A&#x27;</span><br>  &#125;,<br>  &#123;<br>    <span class="hljs-attr">key</span>: <span class="hljs-string">&#x27;拂晓蓝（默认）&#x27;</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">&#x27;#1890FF&#x27;</span><br>  &#125;,<br>  &#123;<br>    <span class="hljs-attr">key</span>: <span class="hljs-string">&#x27;极客蓝&#x27;</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">&#x27;#2F54EB&#x27;</span><br>  &#125;,<br>  &#123;<br>    <span class="hljs-attr">key</span>: <span class="hljs-string">&#x27;酱紫&#x27;</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">&#x27;#722ED1&#x27;</span><br>  &#125;<br>]<br><br><span class="hljs-keyword">const</span> <span class="hljs-title function_">updateTheme</span> = newPrimaryColor =&gt; &#123;<br>  <span class="hljs-keyword">const</span> hideMessage = message.<span class="hljs-title function_">loading</span>(<span class="hljs-string">&#x27;正在切换主题！&#x27;</span>, <span class="hljs-number">0</span>)<br>  themeColor.<span class="hljs-title function_">changeColor</span>(newPrimaryColor).<span class="hljs-title function_">finally</span>(<span class="hljs-function">() =&gt;</span> &#123;<br>    <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;<br>      <span class="hljs-title function_">hideMessage</span>()<br>    &#125;, <span class="hljs-number">10</span>)<br>  &#125;)<br>&#125;<br><span class="hljs-keyword">const</span> <span class="hljs-title function_">updateColorWeak</span> = colorWeak =&gt; &#123;<br>  <span class="hljs-comment">// document.body.className = colorWeak ? &#x27;colorWeak&#x27; : &#x27;&#x27;;</span><br>  <span class="hljs-keyword">const</span> app = <span class="hljs-variable language_">document</span>.<span class="hljs-property">body</span>.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;#app&#x27;</span>)<br>  colorWeak ? app.<span class="hljs-property">classList</span>.<span class="hljs-title function_">add</span>(<span class="hljs-string">&#x27;colorWeak&#x27;</span>) : app.<span class="hljs-property">classList</span>.<span class="hljs-title function_">remove</span>(<span class="hljs-string">&#x27;colorWeak&#x27;</span>)<br>&#125;<br><br><span class="hljs-keyword">export</span> &#123; updateTheme, colorList, updateColorWeak &#125;<br><br></code></pre></td></tr></table></figure>

<p>themeColor.js</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">import</span> client <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;webpack-theme-color-replacer/client&#x27;</span><br><span class="hljs-keyword">import</span> generate <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;@ant-design/colors/lib/generate&#x27;</span><br><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;<br>  getAntdSerials (color) &#123;<br>    <span class="hljs-comment">// 淡化（即less的tint）</span><br>    <span class="hljs-keyword">const</span> lightens = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Array</span>(<span class="hljs-number">9</span>).<span class="hljs-title function_">fill</span>().<span class="hljs-title function_">map</span>(<span class="hljs-function">(<span class="hljs-params">t, i</span>) =&gt;</span> &#123;<br>      <span class="hljs-keyword">return</span> client.<span class="hljs-property">varyColor</span>.<span class="hljs-title function_">lighten</span>(color, i / <span class="hljs-number">10</span>)<br>    &#125;)<br>    <span class="hljs-comment">// colorPalette变换得到颜色值</span><br>    <span class="hljs-keyword">const</span> colorPalettes = <span class="hljs-title function_">generate</span>(color)<br>    <span class="hljs-keyword">const</span> rgb = client.<span class="hljs-property">varyColor</span>.<span class="hljs-title function_">toNum3</span>(color.<span class="hljs-title function_">replace</span>(<span class="hljs-string">&#x27;#&#x27;</span>, <span class="hljs-string">&#x27;&#x27;</span>)).<span class="hljs-title function_">join</span>(<span class="hljs-string">&#x27;,&#x27;</span>)<br>    <span class="hljs-keyword">return</span> lightens.<span class="hljs-title function_">concat</span>(colorPalettes).<span class="hljs-title function_">concat</span>(rgb)<br>  &#125;,<br>  changeColor (newColor) &#123;<br>    <span class="hljs-keyword">var</span> options = &#123;<br>      <span class="hljs-attr">newColors</span>: <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">getAntdSerials</span>(newColor), <span class="hljs-comment">// new colors array, one-to-one corresponde with `matchColors`</span><br>      changeUrl (cssUrl) &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-string">`/<span class="hljs-subst">$&#123;cssUrl&#125;</span>`</span> <span class="hljs-comment">// while router is not `hash` mode, it needs absolute path</span><br>      &#125;<br>    &#125;<br>    <span class="hljs-keyword">return</span> client.<span class="hljs-property">changer</span>.<span class="hljs-title function_">changeColor</span>(options, <span class="hljs-title class_">Promise</span>)<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>


</li>
<li><p>配置vue.config.js</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> createThemeColorReplacerPlugin = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;./config/plugin.config&#x27;</span>)<br><span class="hljs-keyword">const</span> &#123; defineConfig &#125; = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;@vue/cli-service&#x27;</span>)<br><br><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-title function_">defineConfig</span>(&#123;<br>    <span class="hljs-attr">transpileDependencies</span>: <span class="hljs-literal">true</span>,<br>    <span class="hljs-attr">configureWebpack</span>: &#123;<br>        <span class="hljs-comment">// webpack plugins</span><br>        <span class="hljs-attr">plugins</span>: [<br>          <span class="hljs-title function_">createThemeColorReplacerPlugin</span>()<br>        ],<br>        <span class="hljs-comment">// if prod, add externals</span><br>        <span class="hljs-attr">externals</span>: &#123;&#125;<br>      &#125;,<br>    <span class="hljs-attr">css</span>: &#123;<br>    <span class="hljs-attr">loaderOptions</span>: &#123;<br>      <span class="hljs-attr">less</span>: &#123;<br><br>        <span class="hljs-attr">modifyVars</span>:&#123;&#125;,<br>        <span class="hljs-comment">// DO NOT REMOVE THIS LINE</span><br>        <span class="hljs-attr">javascriptEnabled</span>: <span class="hljs-literal">true</span><br>      &#125;<br>    &#125;<br>  &#125;<br>&#125;)<br><br></code></pre></td></tr></table></figure>

<p>至此配置基本完成</p>
</li>
<li><p>定制主题</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;item&quot;</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;item in colorList&quot;</span> <span class="hljs-attr">:key</span>=<span class="hljs-string">&quot;item.key&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">a-button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;setColor(item.color)&quot;</span>&gt;</span>&#123;&#123; item.key &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">a-button</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> &#123; updateTheme,colorList &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./settingConfig&#x27;</span></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span>&#123;</span><br><span class="language-javascript">    <span class="hljs-title function_">data</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-keyword">return</span> &#123;</span><br><span class="language-javascript">            <span class="hljs-attr">primartColor</span>: <span class="hljs-string">&#x27;#1890FF&#x27;</span>,</span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-attr">methods</span>:&#123;</span><br><span class="language-javascript">        <span class="hljs-title function_">setColor</span>(<span class="hljs-params">color</span>) &#123;</span><br><span class="language-javascript">            <span class="hljs-title function_">updateTheme</span>(color)</span><br><span class="language-javascript">        &#125;,</span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure></li>
</ol>
<p>差不多就酱婶</p>

              
            </div>
            <hr/>
            <div>
              <div class="post-metas my-3">
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/blog/tags/antDesign/">#antDesign</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Vue2+AntD切换主题</div>
      <div>https://gitee.com/swallow_week/blog/2022/07/06/动态切换主题/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Fantuan</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年7月6日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/blog/2022/07/06/%E5%8A%A8%E6%80%81%E5%88%87%E6%8D%A2%E4%B8%BB%E9%A2%98%E5%8F%8A%E5%85%B6%E4%BB%96/" title="动态切换主题及其他">
                        <span class="hidden-mobile">动态切换主题及其他</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  






    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/blog/js/events.js" ></script>
<script  src="/blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>






  
    <script  src="/blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script>
  (function() {
    var enableLang = CONFIG.code_language.enable && CONFIG.code_language.default;
    var enableCopy = CONFIG.copy_btn;
    if (!enableLang && !enableCopy) {
      return;
    }

    function getBgClass(ele) {
      return Fluid.utils.getBackgroundLightness(ele) >= 0 ? 'code-widget-light' : 'code-widget-dark';
    }

    var copyTmpl = '';
    copyTmpl += '<div class="code-widget">';
    copyTmpl += 'LANG';
    copyTmpl += '</div>';
    jQuery('.markdown-body pre').each(function() {
      var $pre = jQuery(this);
      if ($pre.find('code.mermaid').length > 0) {
        return;
      }
      if ($pre.find('span.line').length > 0) {
        return;
      }

      var lang = '';

      if (enableLang) {
        lang = CONFIG.code_language.default;
        if ($pre[0].children.length > 0 && $pre[0].children[0].classList.length >= 2 && $pre.children().hasClass('hljs')) {
          lang = $pre[0].children[0].classList[1];
        } else if ($pre[0].getAttribute('data-language')) {
          lang = $pre[0].getAttribute('data-language');
        } else if ($pre.parent().hasClass('sourceCode') && $pre[0].children.length > 0 && $pre[0].children[0].classList.length >= 2) {
          lang = $pre[0].children[0].classList[1];
          $pre.parent().addClass('code-wrapper');
        } else if ($pre.parent().hasClass('markdown-body') && $pre[0].classList.length === 0) {
          $pre.wrap('<div class="code-wrapper"></div>');
        }
        lang = lang.toUpperCase().replace('NONE', CONFIG.code_language.default);
      }
      $pre.append(copyTmpl.replace('LANG', lang).replace('code-widget">',
        getBgClass($pre[0]) + (enableCopy ? ' code-widget copy-btn" data-clipboard-snippet><i class="iconfont icon-copy"></i>' : ' code-widget">')));

      if (enableCopy) {
        Fluid.utils.createScript('https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js', function() {
          var clipboard = new window.ClipboardJS('.copy-btn', {
            target: function(trigger) {
              var nodes = trigger.parentNode.childNodes;
              for (var i = 0; i < nodes.length; i++) {
                if (nodes[i].tagName === 'CODE') {
                  return nodes[i];
                }
              }
            }
          });
          clipboard.on('success', function(e) {
            e.clearSelection();
            e.trigger.innerHTML = e.trigger.innerHTML.replace('icon-copy', 'icon-success');
            setTimeout(function() {
              e.trigger.innerHTML = e.trigger.innerHTML.replace('icon-success', 'icon-copy');
            }, 2000);
          });
        });
      }
    });
  })();
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/blog/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
